﻿using FunArts.EasyDevOps.Shared.Models.SshClient.CommandResult;
using FunArts.EasyDevOps.Shared.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunArts.EasyDevOps.Shared.Models.SshClient
{
    /// <summary>
    /// Linux命令返回结果数据
    /// </summary>
    public interface ILinuxCommandResultData<T>
    {
        /// <summary>
        /// 结果解析
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual LinuxCommandResultBase<List<T>> ParseResultDataList(LinuxCommandResultBase linuxCommandResultBase)
        {
            if (linuxCommandResultBase.ExitStatus != 0)
            {
                if (!linuxCommandResultBase.Error.IsNullOrWhiteSpace())
                {
                    throw new Exception(linuxCommandResultBase.Error);
                }
            }
            if (linuxCommandResultBase.Output.IsNullOrWhiteSpace())
            {
                return new LinuxCommandResultBase<List<T>>()
                {
                    ExitStatus = linuxCommandResultBase.ExitStatus,
                    Error = linuxCommandResultBase.Error,
                    OutData = new List<T>(),
                    Output = linuxCommandResultBase.Output,
                };
            }
            return new LinuxCommandResultBase<List<T>>()
            {
                ExitStatus = linuxCommandResultBase.ExitStatus,
                Error = linuxCommandResultBase.Error,
                OutData = ParseResultDataList(linuxCommandResultBase.Output),
                Output=linuxCommandResultBase.Output,
            };
        }

        /// <summary>
        /// 解析结果具体实现
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="outData"></param>
        /// <returns></returns>
        public List<T> ParseResultDataList(string outData);
        
    }

}
